package com.chhlz.module.base.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.chhlz.core.action.BaseAction;
import com.chhlz.core.action.Json;
import com.chhlz.core.util.ExceptionUtil;
import com.chhlz.module.base.model.Dish;
import com.chhlz.module.base.vo.DishVO;
import com.chhlz.module.service.DishService;
import com.opensymphony.xwork2.ModelDriven;


/**
 * 菜式控制器
 * @author ALEXGORDON
 *
 */
@Controller
@Action(value = "dish", results = { @Result(name = "dish", location = "/dish.html") })
public class DishAction extends BaseAction implements ModelDriven<DishVO>{
	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(DishAction.class);
	@Autowired
	private DishService dishService;
	public DishVO dishVO = new DishVO();
	/**
	 * 得到数据列表
	 */
	public void grid(){
		int total = dishService.getTotal();
		List<Dish> list = dishService.findAll(page, rows, sort, order);	
		writeJson(total,list);
	}
	/**
	 * 添加操作
	 */
	public void add(){
			Json result = new Json();
			try {
				dishService.save(dishVO.toDish());
				result.setSuccess(true);
				result.setMsg("添加菜式成功");					
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加菜式失败");					
			}
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	public void edit() {
//		HttpServletRequest request = this.getRequest();
//		String pr = request.getParameter("dish");
//		System.out.println("dish action edit: " + pr);
//		Enumeration en = request.getParameterNames();
//		while(en.hasMoreElements()){			
//			System.out.println("dish action edit: " + en.nextElement().toString());
//		}
//		
//		System.out.println("dish action edit: " + dishVO.toString());
		
			Json result = new Json();
			try {
				dishService.update(dishVO.toDish());
				result.setSuccess(true);
				result.setMsg("修改菜式成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加菜式失败");
			}
			writeJson(result);
	}
	/**
	 * 删除操作
	 */
	public void remove() {
//		System.out.println("dish action remove: " + dishVO);
			Json result = new Json();
			try {
				dishService.delete(Integer.valueOf(dishVO.getDishId()));
				result.setSuccess(true);
				result.setMsg("删除菜式成功");
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除菜式失败");
			}
			writeJson(result);
	}
	@Override
	public DishVO getModel() {
		return dishVO;
	}
}
